#include "Poki.h"
#include <esp_sleep.h>
#include <esp_pm.h>
#include <math.h>

void lv_img_set_x_center(lv_obj_t * obj, lv_coord_t degree)
{
  int y = 150-150*cos(float(degree)/100.0);
  int x = 150*sin(float(degree)/100.0);
  lv_obj_align(obj, NULL, LV_ALIGN_CENTER, x, y);
}

Poki::Poki()
{
  timecal = millis();
  save_cfg.begin("Config");
    // APP图标页
  app_scr = lv_obj_create(NULL, NULL);
  lv_obj_set_style_local_bg_color(app_scr, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
  lv_scr_load(app_scr);  /*加载屏幕*/
  lv_anim_path_init(&path);
  lv_anim_path_set_cb(&path, lv_anim_path_ease_out);
}

void Poki::setup_ani(Hardware* hardware)
{
  lv_style_init(&font_style);
  lv_style_set_text_font(&font_style, LV_STATE_DEFAULT, &lv_font_montserrat_34);
  lv_style_set_text_color(&font_style, LV_STATE_DEFAULT, LV_COLOR_WHITE);

  lv_style_init(&font_style_app);
  lv_style_set_text_font(&font_style_app, LV_STATE_DEFAULT, &huakang);
  lv_style_set_text_color(&font_style_app, LV_STATE_DEFAULT, LV_COLOR_WHITE);

  lv_obj_t *label = lv_label_create(app_scr, NULL);
  lv_obj_add_style(label, LV_LABEL_PART_MAIN, &font_style);
  lv_label_set_text(label, LV_SYMBOL_HOME  " POKIUI");
  lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, 0);
  lv_task_handler();

  hardware->init_hardware();

  now_app_icon = lv_img_create(app_scr, NULL);
  lv_img_set_src(now_app_icon, &app_icon[now_app]);
  lv_obj_align(now_app_icon, NULL, LV_ALIGN_CENTER, 0, 0);
  lv_obj_fade_in(now_app_icon, 800, 0);
  lv_obj_fade_out(label, 800, 0);
  lv_task_handler();

  now_app_label = lv_label_create(app_scr, NULL);
  lv_obj_add_style(now_app_label, LV_LABEL_PART_MAIN, &font_style_app);

  lv_label_set_text(now_app_label, " ");
  lv_obj_align(now_app_label, NULL, LV_ALIGN_CENTER, 0, 90);
  name_hidden_time = millis();
  lv_task_handler();

  next_app_icon = lv_img_create(app_scr, NULL);
  lv_obj_align(next_app_icon, NULL, LV_ALIGN_CENTER, 0, 480);

  next_app_label = lv_label_create(app_scr, NULL);
  lv_obj_add_style(next_app_label, LV_LABEL_PART_MAIN, &font_style_app);
  lv_obj_align(next_app_label, NULL, LV_ALIGN_CENTER, 0, 480);
  lv_obj_del(label);
}

Event Poki::main_process(Action act)
{
  cur_act = act;
  cur_event = NOEVENT;
  if (is_running_app&&cur_act!=BACK)
    run_app();
  else if (is_running_app&&cur_act==BACK)
    kill_app();
  else if (cur_act==FORWARD)
    setup_app();
  else if (cur_act==TURN_LEFT||cur_act==TURN_RIGHT)
    main_menu();
  if(cur_act!=NOACT&&cur_event == NOEVENT)
    cur_event = RGB_CHANGE;
  if (!is_running_app&&!name_hidden&&millis()-name_hidden_time>2000)
  {
    name_hidden = true;
    lv_label_set_text(now_app_label, " ");
    lv_task_handler();
  }
  return cur_event;
}

void Poki::main_menu()
{
  now_app = (now_app + app_num + 1 - (cur_act==TURN_RIGHT)*2)%app_num;
  
  String app_name_now = app_name[now_app];
  lv_label_set_text(next_app_label, app_name[now_app]);
  lv_obj_align(next_app_label, NULL, LV_ALIGN_CENTER, 0, 90);
  
  lv_img_set_src(next_app_icon, &app_icon[now_app]);
  lv_obj_fade_out(now_app_label, 700, 0);
  lv_obj_fade_in(next_app_label, 700, 0);
  lv_img_set_zoom(next_app_icon, LV_IMG_ZOOM_NONE);
  lv_img_set_zoom(now_app_icon, LV_IMG_ZOOM_NONE/1.4);
  lv_obj_fade_out(now_app_icon, 400, 0);
  lv_obj_fade_in(next_app_icon, 0, 0);
  
  lv_anim_t next_app_ani;
  lv_anim_t now_app_ani;

  lv_anim_init(&next_app_ani);
  lv_anim_init(&now_app_ani);

  lv_anim_set_exec_cb(&next_app_ani, (lv_anim_exec_xcb_t)lv_img_set_x_center);
  lv_anim_set_exec_cb(&now_app_ani, (lv_anim_exec_xcb_t)lv_img_set_x_center);

  lv_anim_set_var(&next_app_ani, next_app_icon);
  lv_anim_set_var(&now_app_ani, now_app_icon);

  lv_anim_set_values(&next_app_ani, (3.14/2-3.14*float(cur_act==TURN_RIGHT))*100, 0);
  lv_anim_set_values(&now_app_ani, 0, (-3.14/2+3.14*float(cur_act==TURN_RIGHT))*100);

  lv_anim_set_time(&next_app_ani, duration);
  lv_anim_set_time(&now_app_ani, duration);

  lv_anim_set_path(&next_app_ani, &path); 
  lv_anim_set_path(&now_app_ani, &path);

  lv_anim_start(&next_app_ani);
  lv_anim_start(&now_app_ani);

  while (lv_anim_count_running())
    lv_task_handler();
  
  lv_obj_align(next_app_icon, NULL, LV_ALIGN_CENTER, 0, 0);
  lv_obj_align(now_app_icon, NULL, LV_ALIGN_CENTER, 0, 480);
  lv_obj_align(next_app_label, NULL, LV_ALIGN_CENTER, 0, 90);
  lv_obj_align(now_app_label, NULL, LV_ALIGN_CENTER, 0, 480);
  lv_task_handler();

  lv_obj_t *tem_app = next_app_icon;
  next_app_icon = now_app_icon;
  now_app_icon = tem_app;

  tem_app = next_app_label;
  next_app_label = now_app_label;
  now_app_label = tem_app;

  name_hidden = false;
  name_hidden_time = millis();
}